/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import dto.Item;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

/**
 *
 * @author Scott Kellish
 */
public class ItemDAO {

    private SqlSessionFactory sqlSessionFactory;

    public ItemDAO() {
        sqlSessionFactory = MyBatisConnectionFactory.getSqlSessionFactory();
    }

    /**
     * Returns a list of all Item instances from the database
     *
     * @return the list of all Item instances from the database
     */
    public List<Item> getAll() {
        SqlSession session = sqlSessionFactory.openSession();
        try {
            List<Item> list = session.selectList("Item.getAll");
            return list;
        } finally {
            session.close();
        }
    }

    /**
     * Returns an Item instance from the database
     *
     * @param id primary key value used for the lookup
     * @return A Item instance with a primary key value equals to pk, or null
     * if there is no match
     */
    public Item getById(int id) {
        SqlSession session = sqlSessionFactory.openSession();
        try {
            Item itemDTO = (Item) session.selectOne("Item.getById", id);
            return itemDTO;
        } finally {
            session.close();
        }
    }

    /**
     * * Updates an instance of Item in the database.
     *
     * @param item the instance to be updated.
     */
    public void update(Item item) {
        SqlSession session = sqlSessionFactory.openSession();
        try {
            session.update("Item.update", item);
            session.commit();
        } finally {
            session.close();
        }
    }

    /**
     * * Insert an instance of Item into the database.
     *
     * @param item the instance to be persisted.
     */
    public void insert(Item item) {
        SqlSession session = sqlSessionFactory.openSession();
        try {
            session.insert("Item.insert", item);
            session.commit();
        } finally {
            session.close();
        }
    }

    /**
     * * Delete an instance of Item from the database.
     *
     * @param id primary key value of the instance to be deleted.
     */
    public void delete(int id) {
        SqlSession session = sqlSessionFactory.openSession();
        try {
            session.delete("Item.deleteById", id);
            session.commit();
        } finally {
            session.close();
        }
    }
    
    /**
     * Truncates the item table and resets the next rowid() back to 1
     */
    public void truncate() {
        SqlSession session = sqlSessionFactory.openSession();
        try {
            session.delete("Item.truncateDelete");
            session.delete("Item.truncateResetRowId");
            session.commit();
        } finally {
            session.close();
        }        
    }
    
}
